<template>
  <PageWrapper>
    <a-card :bordered="false" class="mb-3 proCard">
      <a-space align="center">
        <a-select
          style="width: 220px"
          v-model="params.noticeType"
          :options="noticeTypeOptions"
          placeholder="请选择通知类型"
          clearable
        />
        <a-button type="primary" @click="reloadTable">
          <template #icon>
            <a-icon>
              <SearchOutlined />
            </a-icon>
          </template>
          查询
        </a-button>
      </a-space>
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        row-key="postId"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        virtual-scroll
      />
    </a-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { myNotice } from '@/api/notice/notice';
  import { SearchOutlined } from '@vicons/antd';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';

  const basicTableRef = ref();
  const rowKeys = ref([]);
  const rowKeysName = ref([]);
  const tableData = ref();
  const router = useRouter();

  const noticeTypeOptions = ref([
    {
      label: '公告',
      value: 1,
    },
    {
      label: '会议',
      value: 2,
    },
    {
      label: '其他',
      value: 3,
    },
  ]);

  const params = reactive({
    noticeType: undefined,
  });

  const actionColumn: BasicColumn = reactive({
    width: 150,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render({ record }) {
      return h(TableAction as any, {
        actions: [
          {
            label: '查看',
            onClick: handleView.bind(null, record),
          },
        ],
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await myNotice({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };

  function onCheckedRow(keys) {
    rowKeys.value = keys;
    rowKeysName.value = tableData.value
      .filter((item) => {
        return keys.includes(item.id);
      })
      .map((item) => {
        return item.username;
      })
      .join('，');
  }

  function reloadTable() {
    basicTableRef.value.reload();
  }

  function handleView(record) {
    console.log(record);
    router.push(`/instation/notice/info/${record.noticeId}`);
  }
</script>

<style lang="less" scoped></style>
